qx.Class.define("caraffa.widgets.CheckPlanilla",
{
	extend : qx.ui.core.Widget,
	construct : function ()
	{
		this.base(arguments);

		this._setLayout(new qx.ui.layout.Basic());
		
		this._cmbOS = new qx.ui.form.ow.ComboFilter("Obra Social:", "", true);
		this._cmbPlanes = new qx.ui.form.ow.ComboFilter("Planes:", "", true);

		this._grpRegistros = new qx.ui.groupbox.GroupBox("");
			this._grpRegistros.setWidth(650);
			this._grpRegistros.setHeight(400);
			this._grpRegistros.setLayout(new qx.ui.layout.Basic());
			this._layRegistros = new qx.ui.form.ow.Scroll(new qx.ui.layout.VBox(5), 625, 365);
			this._grpRegistros.add(this._layRegistros, {left:0, top:10});
		
        var model = new qx.ui.table.model.Simple();
		model.setColumns(["cod_planilla_cobro", "Factura", "Fecha", "Hora"]);
		model.setColumnSortable(1, false);
		model.setColumnSortable(2, false);
		model.setColumnSortable(3, false);
		
		this._grdPlanillas = new qx.ui.table.Table(model);
		this._grdPlanillas.setColumnVisibilityButtonVisible(false);
		this._grdPlanillas.setStatusBarVisible(false);
		this._grdPlanillas.getTableColumnModel().setColumnVisible(0, false);
		this._grdPlanillas.setColumnWidth(1, 165);
		this._grdPlanillas.setColumnWidth(2, 75);
		this._grdPlanillas.setColumnWidth(3, 60);
		this._grdPlanillas.setWidth(320);
		this._grdPlanillas.setHeight(335);
		this._btnGuardarCambios = new qx.ui.form.Button("Guardar Cambios");
		this._btnGuardarCambios.setEnabled(false);
		
		this._cmbFormaCobro = new qx.ui.form.ow.ComboBox("Forma de Cobro:", [{value:'C', label:'Cheque'}, {value:'D', label:'Deposito'}, {value:'E', label:'Efectivo'}]);
		this._txaObservaciones = new qx.ui.form.ow.TextArea("Observaciones:");
			this._txaObservaciones.getText().setWidth(320);
			this._txaObservaciones.getText().setHeight(60);
		this._cmbFormaCobro.setEnabled(false);
		this._txaObservaciones.setEnabled(false);
		this._totalCobrado = new qx.ui.basic.Label("<b><big>Total Cobrado: $</big><b>").set({rich:true});

		this._attachEvents();
		
		var rpc = new qx.io.remote.Rpc();
		rpc.setTimeout(10000);
		rpc.setUrl("services/");
        rpc.setServiceName("CheckPlanilla");
        try
        {
			var result = rpc.callSync("getObrasSociales");
			this._cmbOS.setNewValues(result, true);
			this._getPlanes();
		}
		catch (ex)
		{
			alert(ex);
		}

		this._add(this._cmbOS, {left:10, top:10});
		this._add(this._cmbPlanes, {left:10, top:40});
		this._add(this._grdPlanillas, {left:10, top:70});
		this._add(this._grpRegistros, {left:350, top:0});
		this._add(this._btnGuardarCambios, {left:220, top:415});
		this._add(this._cmbFormaCobro, {left:350, top:415});
		this._add(this._txaObservaciones, {left:680, top:415});
		this._add(this._totalCobrado, {left:820, top:410});
	},
	members : 
	{
		_registros : null,
		_totalFactura : null,
		_attachEvents : function ()
		{
			this._cmbOS.addListener("changeValue", this._getPlanes, this);
			this._cmbPlanes.addListener("changeValue", this._getDataForm, this);
			this._grdPlanillas.addListener("cellClick", this._getFormRegistros, this);
			this._grdPlanillas.addListener("keyup", function (e)
			{
				if ((e.getKeyIdentifier() == 'Up') || (e.getKeyIdentifier() == 'Down'))
				{
					this._getFormRegistros();
				}
			}, this);
			this._btnGuardarCambios.addListener("execute", this._guardarCambios, this);
		},
		_getPlanes : function ()
		{
			this._cmbPlanes.getCombo().getChildControl("textfield").setValue("");
			if ((this._cmbOS.getValue() != "") && (this._cmbOS.getValue() != undefined))
			{
				var rpc = new qx.io.remote.Rpc();
				rpc.setTimeout(10000);
				rpc.setUrl("services/");
		        rpc.setServiceName("CheckPlanilla");
		        try
		        {
		        	var params = new Object();
		        	params.cod_os = this._cmbOS.getValue();
					var result = rpc.callSync("getPlanes", params);
					this._cmbPlanes.setNewValues(result);
					
					//ADD oscocial || _getPlanes
//					if (this._cmbOS.getValue() == 4 || (this._cmbOS.getValue() == 2) || (this._cmbOS.getValue() == 5)|| (this._cmbOS.getValue() == 6))
//					{
//						this._cmbPlanes.setEnabled(false);
//					}
//					else
//					{
//						this._cmbPlanes.setEnabled(true);
//					}
				}
				catch (ex)
				{
					alert(ex);
				}
			}
			else
			{
				this._cmbPlanes.setValue("");
//				this._cmbPlanes.setEnabled(false);
				this._grdPlanillas.getTableModel().setData("");
				this._btnGuardarCambios.setEnabled(false);
				this._cmbFormaCobro.setEnabled(false);
				this._txaObservaciones.setEnabled(false);
				this._registros = new Object();
				this._layRegistros.getContainer().removeAll();
				this._recalculaTotalFactura();
			}
		},
		_guardarCambios : function ()
		{
			var params = new Object();
			params.tabla = this._cmbOS.getValue();
			params.cod_planilla_cobro = this._grdPlanillas.getTableModel().getValue(0, this._grdPlanillas.getFocusedRow());
			params.forma_cobro = this._cmbFormaCobro.getValue();
			params.observaciones = this._txaObservaciones.getValue();
//			var reg = new Object();
			var regs = new Array();
			for (var i in this._registros)
			{
				var reg = null;
				reg = new Object();
				reg.cod_turno = this._registros[i].cod_turno;
				reg.cobrado = this._registros[i].cobrado.getValue();
				reg.estado = this._registros[i].estado.getValue();
				regs.push(reg);
//				alert(this._registros[i].cod_turno + " - " + this._registros[i].cobrado.getValue() + " - " + this._registros[i].estado.getValue());
			}
			params.registros = regs
			var rpc = new qx.io.remote.Rpc();
			rpc.setTimeout(10000);
			rpc.setUrl("services/");
	        rpc.setServiceName("CheckPlanilla");
	        try
	        {
				var result = rpc.callSync("marcarCobrado", params);
				this._cmbOS.setValue("");
				this._txaObservaciones.setValue("");
			}
			catch (ex)
			{
				alert(ex);
			}
		},
		_getDataForm : function () {
			if (this._cmbOS.getValue() && this._cmbPlanes.getValue()) {
//				this._btnTraerDatosPlanilla.setEnabled(true);
//				this._btnGuardarCambios.setEnabled(true);
				var rpc = new qx.io.remote.Rpc();
				rpc.setTimeout(10000);
				rpc.setUrl("services/");
		        rpc.setServiceName("CheckPlanilla");
		        var params = new Object();
		        params.cod_os = this._cmbOS.getValue();
		        params.cod_plan = this._cmbPlanes.getValue();
		        try
		        {
					var result = rpc.callSync("getPlanillasOS", params);
					this._grdPlanillas.getTableModel().setData(result);
					this._btnGuardarCambios.setEnabled(false);
					this._cmbFormaCobro.setEnabled(false);
					this._txaObservaciones.setEnabled(false);
					this._registros = new Object();
					this._layRegistros.getContainer().removeAll();
					this._recalculaTotalFactura();
				}
				catch (ex)
				{
					alert(ex);
				}
			}
			this._grdPlanillas.resetSelection();
			this._grdPlanillas.resetCellFocus();
		},
		_getFormRegistros : function ()
		{
			if (this._grdPlanillas.getFocusedRow() != null)
			{
				this._btnGuardarCambios.setEnabled(true);
				this._cmbFormaCobro.setEnabled(true);
				this._txaObservaciones.setEnabled(true);
				this._registros = new Object();
				this._layRegistros.getContainer().removeAll();
				
				var layRow = new qx.ui.container.Composite(new qx.ui.layout.HBox(3));
				layRow.add(new qx.ui.basic.Label("<b>Fecha</b>").set({width:60, rich:true}));
				layRow.add(new qx.ui.basic.Label("<b>Paciente</b>").set({width:120, rich:true}));
				layRow.add(new qx.ui.basic.Label("<b>Prestacion</b>").set({width:200, rich:true}));
				layRow.add(new qx.ui.basic.Label("<b>Presentado</b>").set({width:60, rich:true}));
				layRow.add(new qx.ui.basic.Label("<b>Cobrado</b>").set({width:70, rich:true}));
				layRow.add(new qx.ui.basic.Label("<b>Estado</b>").set({width:100, rich:true}));
				this._layRegistros.add(layRow);
	
				var regs = this._getRegistros(this._grdPlanillas.getTableModel().getValue(0, this._grdPlanillas.getFocusedRow()));
				for (var i in regs)
				{
					this._layRegistros.add(this._getObjectsChecks(regs[i]));
				}
				
//				for (var i=0; i<=20; i++)
//				{
//					layRegistros.add(new qx.ui.form.TextField());
//				}
				
				this._recalculaTotalFactura();
			}
		},
		_getRegistros : function (cod_planilla_cobro)
		{
			var rpc = new qx.io.remote.Rpc();
			rpc.setTimeout(10000);
			rpc.setUrl("services/");
	        rpc.setServiceName("CheckPlanilla");
	        var params = new Object();
	        params.os = this._cmbOS.getValue();
	        params.cod_planilla_cobro = cod_planilla_cobro;
	        try
	        {
				var result = rpc.callSync("getRegistros", params);
				return result;
			}
			catch (ex)
			{
				alert(ex);
			}
		},
		_getObjectsChecks : function (reg)
		{
			
			this._registros[reg.cod_turno] = new Object();
			var layRow = new qx.ui.container.Composite(new qx.ui.layout.HBox(3));
			layRow.add(new qx.ui.basic.Label(reg.fecha).set({width:60}));
			layRow.add(new qx.ui.basic.Label(reg.paciente).set({width:120}));
			layRow.add(new qx.ui.basic.Label(reg.prestacion).set({width:200}));
			layRow.add(new qx.ui.basic.Label(reg.importe).set({width:60}));
			
			this._registros[reg.cod_turno].cod_turno = reg.cod_turno;
			
			this._registros[reg.cod_turno].cobrado = new qx.ui.form.TextField(reg.importe).set({width:70});
			layRow.add(this._registros[reg.cod_turno].cobrado);
			this._registros[reg.cod_turno].cobrado.addListener("focusout", this._recalculaTotalFactura, this);
			
			this._registros[reg.cod_turno].estado = new qx.ui.form.ow.ComboBox("", [{value:"C", label:"Cobrado", selected:true}, {value:"R", label:"Recobrar"}, {value:"I", label:"Incobrable"}]);
			this._registros[reg.cod_turno].estado.setUserData("cod_turno", reg.cod_turno);
			this._registros[reg.cod_turno].estado.getLabel().setWidth(0);
			this._registros[reg.cod_turno].estado.getCombo().setWidth(80);
			this._registros[reg.cod_turno].estado.addListener("changeValue", function (e)
			{
				if (this._registros[reg.cod_turno].estado.getValue() != 'C')
				{
					this._registros[reg.cod_turno].cobrado.setEnabled(false);
				}
				else
				{
					this._registros[reg.cod_turno].cobrado.setEnabled(true);
				}
				this._recalculaTotalFactura();
			}, this);
			layRow.add(this._registros[reg.cod_turno].estado);
			return layRow;
		},
		_recalculaTotalFactura : function ()
		{
			var totalFactura = 0;
			for (var j in this._registros)
			{
				if (this._registros[j].cobrado.isEnabled())
				{
					totalFactura += parseFloat(this._registros[j].cobrado.getValue());
				}
			}
			if (isNaN(totalFactura))
				totalFactura = 0;
				
			totalFactura=Math.round(totalFactura*100)/100
			totalFactura=totalFactura.toFixed(2)
			this._totalCobrado.setValue("<b><big>Total Cobrado: $</big><b>" + totalFactura + "");
		}
	}
});
